﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QuadRemote;
using QuadRemote.Core;
using System.Drawing;
using System.Windows;
using System.Drawing.Drawing2D;

namespace QuadRemote.Panels
{
    class GamepadPanel : QuadPanel
    {
        public GamepadPanel() : base("Gamepad", 180, 80, Color.White,50)
        {

        }


        public override void PaintPanel(Graphics g)
        {
            //GamepadController gamepad = GamepadController.instance();
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

            Pen p = Pens.Gray;
            /*
            if (gamepad.isConnected() && this.panelColor != Color.Lime)
            {
                this.panelColor = Color.Lime;
                reposition();
            }
            else if (!gamepad.isConnected() && this.panelColor != Color.Red)
            {
                this.panelColor = Color.Red;
                reposition();
            }*/

                int thumbMargin = 10;

                int thumbWidth = PanelWidth / 2 - thumbMargin;
                int thumbHeight = PanelHeight;
                int thumbRadius = thumbWidth / 4;
                int buttonRadius = thumbWidth / 3;
                int dpadRadius = 4;
                
                int t1X = PanelWidth / 2 - thumbWidth / 2 - thumbMargin;
                int t1Y = PanelHeight / 2;
                int t2X = PanelWidth / 2 + thumbWidth / 2 + thumbMargin;
                int t2Y = PanelHeight / 2;

                int triggerHeight = PanelHeight - 2 * thumbMargin;
                int triggerCenter = triggerHeight / 2;
                g.DrawLine(p, t1X - thumbWidth / 2, t1Y, t1X + thumbWidth / 2, t1Y);
                g.DrawLine(p, t2X - thumbWidth / 2, t2Y, t2X + thumbWidth / 2, t2Y);
                g.DrawLine(p, t1X, t1Y - thumbHeight / 2, t1X, t1Y + thumbHeight / 2);
                g.DrawLine(p, t2X, t2Y - thumbHeight / 2, t2X, t2Y + thumbHeight / 2);
                
            /*
                if (gamepad.isButton(0)) // green
                {
                    g.FillEllipse(Brushes.Green, t2X - buttonRadius / 2, t2Y + thumbHeight / 4 - buttonRadius / 2, buttonRadius, buttonRadius);
                }
                if (gamepad.isButton(1)) // red
                {
                    g.FillEllipse(Brushes.Red, t2X + thumbWidth / 4 - buttonRadius / 2, t2Y - buttonRadius / 2, buttonRadius, buttonRadius);
                }
                if (gamepad.isButton(2)) // blue
                {
                    g.FillEllipse(Brushes.Blue, t2X - thumbWidth / 4 - buttonRadius/2, t2Y - buttonRadius/2, buttonRadius, buttonRadius);
                }
                if (gamepad.isButton(3)) // yellow
                {
                    g.FillEllipse(Brushes.Yellow, t2X - buttonRadius/2, t2Y - thumbHeight / 4 - buttonRadius/2, buttonRadius, buttonRadius);
                }
                if (gamepad.isButton(4)) // l1
                {
                    //g2.setColor(new Color(200, 200, 200));
                    g.FillRectangle(Brushes.White, t1X - 10, t1Y - thumbHeight / 2, 20, 4);
                }
                if (gamepad.isButton(5)) // r1
                {
                    g.FillRectangle(Brushes.White, t2X - 10, t2Y - thumbHeight / 2, 20, 4);
                }
                if (gamepad.isButton(6)) // back/select
                {
                    g.FillEllipse(Brushes.White, PanelWidth / 2 - thumbMargin - 4, thumbMargin, 8, 8);
                }
                if (gamepad.isButton(7)) // start
                {
                    g.FillEllipse(Brushes.White, PanelWidth / 2 + thumbMargin - 4, thumbMargin, 8, 8);
                }
                if (gamepad.isButton(10)) // big button
                {
                    g.FillEllipse(Brushes.Violet, PanelWidth / 2 - 10, triggerCenter - 10, 20, 20);
                }

                Vector axis = gamepad.leftThumb();
                g.FillEllipse(gamepad.isButton(8) ? Brushes.Red : Brushes.White, t1X + (int)(axis.X * (thumbWidth / 2 - thumbRadius / 2)) - thumbRadius / 2, (int)(t1Y - axis.Y * (thumbHeight / 2 - thumbRadius / 2)) - thumbRadius / 2, thumbRadius, thumbRadius);
                
                axis = gamepad.rightThumb();
                g.FillEllipse(gamepad.isButton(9) ? Brushes.Red : Brushes.White, t2X + (int)(axis.X * (thumbWidth / 2 - thumbRadius / 2)) - thumbRadius / 2, (int)(t2Y - axis.Y * (thumbHeight / 2 - thumbRadius / 2)) - thumbRadius / 2, thumbRadius, thumbRadius);
                
                axis = gamepad.dpad();
                g.FillRectangle(Brushes.White, PanelWidth / 2 + (int)(axis.X * thumbMargin) - dpadRadius / 2, PanelHeight - thumbMargin + (int)(axis.Y * thumbMargin) - dpadRadius / 2, dpadRadius, dpadRadius);



                double right = gamepad.rightTrigger();
                g.FillRectangle(Brushes.White, PanelWidth / 2 - thumbMargin / 2, triggerCenter - (int)(right * triggerCenter), thumbMargin, (int)(right * triggerCenter) + 2);
                double left = gamepad.leftTrigger();
                g.FillRectangle(Brushes.White, PanelWidth / 2 - thumbMargin / 2, triggerCenter, thumbMargin, (int)(left * triggerCenter) + 2);

                g.DrawEllipse(Pens.White, t1X - thumbWidth / 2, t1Y - thumbHeight / 2, thumbWidth, thumbHeight);
                g.DrawEllipse(Pens.White, t2X - thumbWidth / 2, t2Y - thumbHeight / 2, thumbWidth, thumbHeight);
                g.DrawRectangle(Pens.White, PanelWidth / 2 - thumbMargin / 2, 0, thumbMargin, triggerHeight);
                g.DrawRectangle(Pens.White, PanelWidth / 2 - thumbMargin, PanelHeight - thumbMargin * 2, thumbMargin * 2, thumbMargin * 2);
                /*g.DrawEllipse(Pens.Gray,0, 0, radius, radius);
                g.DrawEllipse(Pens.Gray, margin + radius, 0, radius, radius);
                g.DrawEllipse(Pens.Gray, (int)(margin / 2) + offset * 2 - radius / 8, offset * 2 - offsetMin / 4 - radius / 8 - 2, radius / 4, radius / 4);
                g.DrawEllipse(Pens.Gray, (int)(margin / 2) + offset * 2 - 6, offsetMin - offsetMin / 2 - 6 - 2, 12, offsetMin + 3);*/
        }
    }
}
